package com.fertshop.gui.listener;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import com.fertshop.dao.FertishopDao;
import com.fertshop.dto.ShopDto;
import com.fertshop.exception.FieldEmptyException;
import com.fertshop.exception.SPDoorNoExistException;
import com.fertshop.exception.StrgpDoorNumberExistException;
import com.fertshop.gui.Main;
import com.fertshop.gui.MainPanel;

public class SubmitActionListener implements ActionListener{
	
	private MainPanel mainPanel;
	private FertishopDao fertishopdao=new FertishopDao();
	
	/**
	 * This listener gets fired when submit button is pressed.
	 * @param mainPanel
	 */
	public SubmitActionListener(MainPanel mainPanel){
		this.mainPanel=mainPanel;		
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		ShopDto shopdto=new ShopDto();
		
		try{
			setShopDto(shopdto);
			fertishopdao.addShop(shopdto);
			JOptionPane.showMessageDialog(Main.frame, "The Shop Details Have Been Stored Successfully") ;
			clearTextFields();
		}catch(Exception e){
			
			if(e instanceof SPDoorNoExistException){				
				showMessageDialog(e);
			}
			
			if(e instanceof StrgpDoorNumberExistException){
				showMessageDialog(e);
			}
			
			if(e instanceof FieldEmptyException){
				showEmptyMessageDialog(e);
			}
		}
		
	}
	
	/**
	 * This method sets the values to the dto.
	 * @param shopdto
	 * @throws Exception
	 */
	private void setShopDto(ShopDto shopdto)throws Exception{
		shopdto.setShopName(mainPanel.getShopNameTf().getText());
		shopdto.setProprietorName(mainPanel.getProprietorNameTf().getText());
		shopdto.setLicenseNumber(mainPanel.getLicenseNoTf().getText());
		shopdto.setAddress(mainPanel.getAddressTa().getText());
		if(mainPanel.getDoorNoTf().getText().isEmpty())
			throw new FieldEmptyException("Sale Point Door Number Cannot be Empty.");
		shopdto.setSpDoorNo(mainPanel.getDoorNoTf().getText());		
		if(mainPanel.getAreaTf().getText().isEmpty())
			throw new FieldEmptyException("Sale Point Area Cannot be Empty.");
		shopdto.setSpArea(mainPanel.getAreaTf().getText());
		shopdto.setStrgpDoorNo(mainPanel.getSpDoorNoTf().getText());
		shopdto.setStrgpArea(mainPanel.getSpAreaTf().getText());
	}
	
	/**
	 * This method clears all the text fields in the MainPanel.
	 */
	private void clearTextFields(){
		mainPanel.getShopNameTf().setText("");
		mainPanel.getProprietorNameTf().setText("");
		mainPanel.getLicenseNoTf().setText("");
		mainPanel.getAddressTa().setText("");
		mainPanel.getDoorNoTf().setText("");
		mainPanel.getAreaTf().setText("");
		mainPanel.getSpDoorNoTf().setText("");
		mainPanel.getSpAreaTf().setText("");
	}
	
	/**
	 * This method shows a dialog and displays the exception message.
	 * @param e Exception.
	 */
	private void showMessageDialog(Exception e){
		JDialog spdialog=new JDialog(Main.frame, true) ;
		Container ctr=spdialog.getContentPane();
		ctr.setLayout(null);
		
		JLabel messageLabel=new JLabel(e.getMessage());
		messageLabel.setBounds(30,30,300,50);
		ctr.add(messageLabel);
		 
		JButton detailButton=new JButton("View Details");
		detailButton.addActionListener(new ViewDetailsActionListener(spdialog));
		detailButton.setBounds(100,85,150,30);
		ctr.add(detailButton);
		
		spdialog.setBounds(100,100,400,200);
		spdialog.setVisible(true);
	}
	
	/**
	 * This method shows message dilog and displays message from exception.
	 * @param e
	 */
	private void showEmptyMessageDialog(Exception e){
		JOptionPane optionpane=new JOptionPane(e.getMessage());
		JDialog dialog=optionpane.createDialog(Main.frame, "Message");		
		dialog.setVisible(true);
	}

}
